package edu.hawaii.duedates.webapp;

import static org.junit.Assert.assertNotNull;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import edu.hawaii.duedates.datastore.DueDatesConfig;
import edu.hawaii.duedates.datastore.User;
import edu.hawaii.duedates.libraries.UhLibrary;

/**
 * Test AlertsPage.
 * @author Daniel Arakaki, Jeho Jung, Arthur Shum, Daniel Tian.
 *
 */
public class TestAlertsPage {

  private static final String form = "form";
  
  private DueDatesConfig config;
  
  /**
   * Set up process before the Test Case. 
   */
  @Before
  public void setUp() {
    this.config = new DueDatesConfig();
    User user = new User("myname", "myemail@example.com","mypassword");
    
    String login1 = System.getProperty("DUEDATES_TESTUSER1_ID");
    String password1 = System.getenv("DUEDATES_TESTUSER1_LASTNAME");
    UhLibrary uh1 = new UhLibrary(login1, password1);
    user.addLibrary(uh1);

    String login2 = System.getenv("DUEDATES_TESTUSER2_ID");
    String password2 = System.getenv("DUEDATES_TESTUSER2_LASTNAME");     
    UhLibrary uh2 = new UhLibrary(login2, password2);     
    user.addLibrary(uh2);
    
    UhLibrary uh3 = new UhLibrary("fakelogin", "fakepassword");
    user.addLibrary(uh3);
    
    this.config.addUser(user);
    this.config.setSmtpServer("mysmtpserver");
  }
  
  /**
   * Test HomePage.
   */
  @Test
  public void testHomePage() {
    // use the alternate DueDatesApplication constructor
    WicketTester tester = new WicketTester(new DueDatesApplication(this.config));
    tester.startPage(LoginPage.class);
    tester.assertRenderedPage(LoginPage.class);
    // login with a test user account
    FormTester formTester = tester.newFormTester(form);
    formTester.setValue("email", "myemail@example.com");
    formTester.setValue("password", "mypassword");
    formTester.submit("login");
    tester.assertRenderedPage(HomePage.class);
    tester.clickLink("alerts");
    tester.assertRenderedPage(AlertsPage.class);
    tester.assertContains("Library");
    
    FormTester formtester1 = tester.newFormTester(form);
    formtester1.submit("enable");
    tester.assertInfoMessages(new String[] { "No libraries selected." });
    
    FormTester formtester2 = tester.newFormTester(form);
    formtester2.select("group", 1);
    formtester2.select("group", 2);
    formtester2.submit("enable");
    tester.clickLink("home");
    tester.assertRenderedPage(HomePage.class);
    tester.clickLink("alerts");
    tester.assertInfoMessages(new String[] { "No libraries selected.", "Alerts enabled.", 
        "Alerts are currently enabled." });

    FormTester formtester3 = tester.newFormTester(form);
    formtester3.submit("disable");

    assertNotNull("logout rendered", tester.getTagByWicketId("logout"));
    tester.clickLink("logout");
    tester.assertRenderedPage(LoginPage.class);
  }
  
}
